﻿using FastReport.DevComponents.DotNetBar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.TongShiWei.COA;
using static WinFormsApp1.TongShiWei.ShowIPQCData;

namespace WinFormsApp1.TongShiWei
{
    public partial class AddCOAData : UIForm
    {
        public AddCOAData()
        {
            InitializeComponent();
            //给检验报告类型添加数据
            ShowCOATypeData();
            //给检验类别添值
            ShowIPQCTypeData();
        }
        #region 给检测类别赋值
        public void ShowIPQCTypeData()
        {
            List<IPQCType_> IPQCTypeData = new List<IPQCType_>()
            {
                new IPQCType_(0,"请选择检验类别"),
                new IPQCType_(1,"自检"),
                new IPQCType_(2,"外检"),
            };
            this.COAQuarantineType.DataSource = IPQCTypeData;
            this.COAQuarantineType.DisplayMember = "IPQCTypeName";
            this.COAQuarantineType.ValueMember = "IPQCTypeId";
        }
        public record IPQCType_(int IPQCTypeId, string IPQCTypeName);
        #endregion
        /// <summary>
        /// 显示生产入库表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton6_Click(object sender, EventArgs e)
        {
            ShowDepotData showDepotData = new ShowDepotData();
            showDepotData.Show();
        }

        private void uiLabel4_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel10_Click(object sender, EventArgs e)
        {

        }
        //
        private void uiButton7_Click(object sender, EventArgs e)
        {
            ShowLivecattle showLivecattle = new ShowLivecattle();
            showLivecattle.Show();
        }
        private void uiButton5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #region  检验报告类型
        //检验报告类型点击事件
        int COATypeData_ = 0;
        private void COAType_SelectedIndexChanged(object sender, EventArgs e)
        {
            COATypeData_ = this.COAType.SelectedIndex;
        }
        //获取所选择的商品信息
        int GoodsId = 0;
        string GoodsName = "";
        //获取非瘟检验商品
        public void GetLivestockDataId(int LivestockId, string LivestockName)
        {
            GoodsId = LivestockId;
            GoodsName = LivestockName;
            ShowCOPIpqcData();
        }
        //获取产品检验商品
        public void GetProductionDataId(int ProductionId, string ProductionName)
        {
            GoodsId = ProductionId;
            GoodsName = ProductionName;
            ShowCOPIpqcData();
        }
        //给检验报告类型添加数据
        public void ShowCOATypeData()
        {
            List<COATypeData> cOATypeData = new List<COATypeData>()
            {
                new COATypeData( 0, "请选择检疫类型"),
                new COATypeData( 1, "产品检验"),
                new COATypeData( 2, "非瘟检验"),
            };
            this.COAType.DataSource = cOATypeData;
            this.COAType.DisplayMember = "COATypeDataName";
            this.COAType.ValueMember = "COATypeDataId";
        }


        //选择商品
        private void uiButton8_Click(object sender, EventArgs e)
        {
            if (COATypeData_ == 0)
            {
                return;
            }
            else if (COATypeData_ == 1)
            {
                //产品检验
                ProductionData productionData = new ProductionData();
                productionData.onGetProductionId += GetProductionDataId;
                productionData.Show();
            }
            else
            {
                //非瘟检验
                LivestockData livestockData = new LivestockData();
                livestockData.OnGetGoodsId += GetLivestockDataId;
                livestockData.Show();
            }
        }



        public record COATypeData(int COATypeDataId, string COATypeDataName);
        #endregion
        #region 显示检测标准表数据
        List<PRQSData> pRQSDatas = new List<PRQSData>();
        public async void ShowCOPIpqcData()
        {
            this.dataGridView1.Rows.Clear();
            GetCOOIPQCData getCOOIPQCData = new GetCOOIPQCData(GoodsId, GoodsName);
            //http://localhost:5159/api/TSW/GetCPOIpqcData
            var res = await HttpClientHelper.RequestUrl<GetCOOIPQCData, ApiResult<List<PRQSData>>>(URLCommon.Url2, "/TSW/GetCPOIpqcData", HttpClientHelper.MethodType.POST, getCOOIPQCData);
            if (res.Code > 0)
            {
                pRQSDatas = res.Data;
                foreach (var item in pRQSDatas)
                {
                    this.dataGridView1.Rows.Add(item.PRQSId, item.PRQSindex, item.PRQSName, item.Require);
                }
            }
        }
        //定义检验项目类
        public class PRQSData
        {
            /// <summary>
            /// 检疫项目Id
            /// </summary>
            public int PRQSId { get; set; }
            /// <summary>
            /// 检疫项目名称
            /// </summary>
            public string PRQSName { get; set; } = string.Empty;
            /// <summary>
            /// 检疫指标
            /// </summary>
            public string PRQSindex { get; set; } = string.Empty;
            /// <summary>
            /// 标准要求
            /// </summary>
            public string Require { get; set; } = string.Empty;
        }
        public record GetCOOIPQCData(int GoodsId, string GoodsName);
        #endregion

        #region 添加图片
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "图片文件(*.jpg;*.jpeg;*.gif;*.bmp;*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        ///新的图片路径
                        string imagePath = openFileDialog.FileName;
                        //并显示出来
                        pictureBox1.Image = Image.FromFile(imagePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("上传出错: " + ex.Message);
                    }
                }
            }
        }
        #endregion

        #region 添加检验报告
        //添加检验报告
        private async void uiButton2_Click(object sender, EventArgs e)
        {
            List<int> IPQCId_ = new List<int>();
            List<string> COAStandardContext_ = new List<string>();
            List<string> COAStandardVerdict_ = new List<string>();
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                IPQCId_ .Add( Convert.ToInt32(this.dataGridView1.Rows[item.Index].Cells[0].Value));
                COAStandardContext_.Add(this.dataGridView1.Rows[item.Index].Cells[4].Value.ToString());
                COAStandardVerdict_.Add(this.dataGridView1.Rows[item.Index].Cells[5].Value.ToString());
            }
            COPData_Now cOPData_Now = new COPData_Now()
            {
                COAId = 0,
                LiveAnimalsDocument = this.LiveAnimalsDocument.Text,
                LiveAnimalsDocumentTime = DateTime.Parse(this.LiveAnimelsDocumentTime.Text),
                COARType = this.COAType.Text,
                COAGoods = GoodsName,
                CheckInBatch = this.CheckInBatch.Text,
                COAQuarantineType = this.COAQuarantineType.Text,
                OriginAddr = this.OriginAddr.Text,
                SampleSize = this.SampleSize.Text,
                SampleNum = Convert.ToInt32(this.SampleNum.Text),
                SampleTime = DateTime.Parse(this.SampleTIme.Text),
                SampleData = this.SampleData.Text,
                COATime = DateTime.Parse(this.COATime.Text),
                ExecuteData = this.ExecuteData.Text,
                COAAuditState = 0,
                COACreateName = "张三",
                COACreateTime = DateTime.Now,
                COAImage = this.pictureBox1.Image.ToString(),
                ProductionName = this.ProductionName.Text,
                COAverdict = this.COAverdict_.Text,
                IPQCId = IPQCId_,
                COAStandardContext = COAStandardContext_,
                COAStandardVerdict = COAStandardVerdict_,
                ISDeleteCOA = false
            };
            //http://localhost:5159/api/TSW/AddCOAIPQCData
            var res =await HttpClientHelper.RequestUrl<COPData_Now, ApiResult<COPData_Now>> (URLCommon.Url1, "/TSW/AddCOAIPQCData", HttpClientHelper.MethodType.POST, cOPData_Now);
            if (res.Code > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        //添加检验报告类
        public class COPData_Now
        {
            /// <summary>
            /// 检疫报告单ID
            /// </summary>
            public int COAId { get; set; }
            /// <summary>
            /// 单据ID
            /// </summary>
            public string LiveAnimalsDocument { get; set; } = string.Empty;
            /// <summary>
            /// 单据日期
            /// </summary>
            public DateTime LiveAnimalsDocumentTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 检验报告类型
            /// </summary>
            public string COARType { get; set; } = string.Empty;
            /// <summary>
            /// 检测商品
            /// </summary>
            public string COAGoods { get; set; } = string.Empty;
            /// <summary>
            /// 商品批号
            /// </summary>
            public string CheckInBatch { get; set; } = string.Empty;
            /// <summary>
            /// 检验类型
            /// </summary>
            public string COAQuarantineType { get; set; } = string.Empty;
            /// <summary>
            /// 样品来源
            /// </summary>
            public string OriginAddr { get; set; } = string.Empty;
            /// <summary>
            /// 样品规格
            /// </summary>
            public string SampleSize { get; set; } = string.Empty;
            /// <summary>
            /// 样品数量
            /// </summary>
            public int SampleNum { get; set; }
            /// <summary>
            /// 到样日期
            /// </summary>
            public DateTime SampleTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 样品形态
            /// </summary>
            public string SampleData { get; set; } = string.Empty;
            /// <summary>
            /// 检验日期
            /// </summary>
            public DateTime COATime { get; set; } = DateTime.Now;
            /// <summary>
            /// 执行标准
            /// </summary>
            public string ExecuteData { get; set; } = string.Empty;
            /// <summary>
            /// 审核状态
            /// </summary>
            public int COAAuditState { get; set; }
            /// <summary>
            /// 制单人
            /// </summary>
            public string COACreateName { get; set; } = string.Empty;
            /// <summary>
            /// 制单日期
            /// </summary>
            public DateTime COACreateTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool ISDeleteCOA { get; set; }
            /// <summary>
            /// 检疫证
            /// </summary>
            public string COAImage { get; set; } = string.Empty;
            /// <summary>
            /// 生产单位
            /// </summary>
            public string ProductionName { get; set; } = string.Empty;
            /// <summary>
            /// 检验结论
            /// </summary>
            public string COAverdict { get; set; } = string.Empty;
            /// <summary>
            /// 检验项目ID
            /// </summary>
            public List<int> IPQCId { get; set; }
            /// <summary>
            /// 检验结果
            /// </summary>
            public List<string> COAStandardContext { get; set; }
            /// <summary>
            /// 单项结论
            /// </summary>
            public List<string> COAStandardVerdict { get; set; }
        }
        #endregion
    }
}
